package com.dengdai.applibrary.activitys;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.interfaces.WebViewInterface;
import com.dengdai.applibrary.utils.BaseViewUtils;
import com.dengdai.applibrary.utils.NetWorkUtils;
import com.dengdai.applibrary.widgets.custom.LoadDataEmptyView;
import com.dengdai.pullrefresh.library.PullToRefreshBase;
import com.dengdai.pullrefresh.library.PullToRefreshWebView;

/**
 * @说明 网页刷新
 * @作者 LY
 * @时间 16/5/21 11:27
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public abstract class BaseWebViewActivity extends BaseActivity implements WebViewInterface {
    protected PullToRefreshWebView pullToRefreshWebView;
    protected LoadDataEmptyView loadDataEmpty;


    @Override
    public int getLayoutResID() {
        return BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.LAYOUT, ViewInfo.BASE_WEBVIEW_ACTIVITY);
    }

    @Override
    public void initData() {
        pullToRefreshWebView = (PullToRefreshWebView) findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.PULLTOREFRESHWEBVIEW));
        loadDataEmpty = (LoadDataEmptyView) findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.LOADDATAEMPTY));
    }

    @Override
    public void business() {
        //设置网页刷新方式
        pullToRefreshWebView.setMode(getWebViewMode());
        //网页设置
        WebSettings webSettings = pullToRefreshWebView.getRefreshableView().getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setGeolocationEnabled(true);
        webSettings.setDomStorageEnabled(true);
        //网页适配HTML5
        GeoClient geo = new GeoClient();
        pullToRefreshWebView.getRefreshableView().setWebViewClient(new PTCWebViewClient(BaseWebViewActivity.this, pullToRefreshWebView.getRefreshableView()));
        pullToRefreshWebView.getRefreshableView().setWebChromeClient(geo);
        String origin = "";
        geo.onGeolocationPermissionsShowPrompt(origin, new GeolocationPermissions.Callback() {
            @Override
            public void invoke(String origin, boolean allow, boolean retain) {

            }
        });
        //网页监听
        pullToRefreshWebView.getRefreshableView().setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    loadDataEmpty.setVisibility(View.GONE);
                    pullToRefreshWebView.onRefreshComplete();
                } else {
                    loadDataEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
        //网页刷新
        pullToRefreshWebView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<WebView>() {
            @Override
            public void onRefresh(PullToRefreshBase<WebView> refreshView) {
                pullToRefreshWebView.getRefreshableView().loadUrl(getLoadUrl());
            }
        });
        //加载网页
        loadData();
    }

    /**
     * 加载网页
     */
    private void loadData() {
        if (NetWorkUtils.getNetworkStatus(BaseApplication.getAppContext())) {
            pullToRefreshWebView.getRefreshableView().loadUrl(getLoadUrl());
            loadDataEmpty.loadingData();
            loadDataEmpty.setVisibility(View.VISIBLE);
        } else {
            loadDataEmpty.setVisibility(View.VISIBLE);
            loadDataEmpty.netWorkError();
        }
    }

    public class GeoClient extends WebChromeClient {
        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            super.onGeolocationPermissionsShowPrompt(origin, callback);
            callback.invoke(origin, true, false);
        }
    }

    public class PTCWebViewClient extends WebViewClient {
        public PTCWebViewClient(Context con, final WebView web) {
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            loadDataEmpty.setVisibility(View.VISIBLE);
            loadDataEmpty.loadDataError();
        }
    }

}
